package com.seu.me.test.dom4j;

import org.apache.commons.lang3.StringUtils;
import org.dom4j.*;
import org.dom4j.io.SAXReader;

import java.io.*;
import java.util.*;

/**
 * @author liangfeihu
 * @since 2019/10/11 11:44
 */
public class ParseXmlOne {

    public static void main(String[] args) throws FileNotFoundException {
        parseXmlOne();
        parseXmlTwo();
        System.out.println("--------------------------");
        parseXmlThree();
    }

    public static void parseXmlOne() {
        //创建SAXReader 的Reader 实例
        SAXReader reader = new SAXReader();
        try {
            Document document = reader.read(new File("/Users/liangfeihu/projects/gitee/myown/multi-learning/xml/test.xml"));
            Element students = document.getRootElement();
            Iterator<Element> it = students.elementIterator();
            while (it.hasNext()) {
                Element student = it.next();
                System.out.println(" 节点 " + student.getName() + " :");
                //遍历所有属性名和属性值
                List<Attribute> student_attr = student.attributes();
                for (Attribute attribute : student_attr) {
                    System.out.println("--- 属性 " + attribute.getName() + " = " + attribute.getValue());
                }

                //遍历所有子节点和节点值
                Iterator<Element> subElement_it = student.elementIterator();
                while (subElement_it.hasNext()) {
                    Element subEle = subElement_it.next();
                    System.out.println("节点：" + subEle.getName() + " = " + subEle.getStringValue());
                }
            }

        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    public static void parseXmlTwo() throws FileNotFoundException {
        String content = readFileContent("/Users/liangfeihu/projects/gitee/myown/multi-learning/xml/test.xml");
        List<HashMap<String, String>> personList = new ArrayList<HashMap<String, String>>();
        try {
            Document document = DocumentHelper.parseText(content);

            // 获取根节点
            Element rootElement = document.getRootElement();
            for (Iterator<?> iterator = rootElement.elementIterator(); iterator.hasNext(); ) {
                Element element = (Element) iterator.next();

                // test
                String nameTrim = element.elementTextTrim("name");
                String name = element.elementText("name");
                System.out.println("name=" + name + ", nameTirm=" + nameTrim);
                String emailTrim = element.elementTextTrim("email");
                String email = element.elementText("email");
                System.out.println("email=" + email + ", emailTirm=" + emailTrim);

                HashMap<String, String> personMap = new HashMap<String, String>();

                //获取元素的gender属性对象
                Attribute genderAttr = element.attribute("gender");
                // 获取属性对象的值并插入map中，键名为gender
                personMap.put("gender", StringUtils.trim(genderAttr.getValue()));

                for (Iterator<?> iterator2 = element.elementIterator(); iterator2.hasNext(); ) {
                    Element element2 = (Element) iterator2.next();

                    // 将子节点的名称和值分别插入map中
                    personMap.put(element2.getName(), element2.getTextTrim());

                    // test
                    String stringValue = element2.getStringValue();
                    String textTrim = element2.getTextTrim();
                    String text = element2.getText();
                    System.out.println("stringValue=" + stringValue + ", textTrim=" + textTrim + ", text=" + text);
                }
                //将map插入list中
                personList.add(personMap);
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        System.out.println(personList);
    }

    public static String readFileContent(String fileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        StringBuffer sbf = new StringBuffer();
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                sbf.append(tempStr);
            }
            reader.close();
            return sbf.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return sbf.toString();
    }

    public static void parseXmlThree() {
        //创建SAXReader 的Reader 实例
        SAXReader reader = new SAXReader();
        try {
            Document document = reader.read(new File("/Users/liangfeihu/projects/gitee/myown/multi-learning/xml/out_SGP0200000000130_1.xml"));
            Element root = document.getRootElement();
            // TB承保信息、BQ保全信息
            String transType = root.elementText("TransType");
            //外包商家标记 0002忆保 0003沛合 0004 栈略
            String outName = root.elementText("OutName");
            System.out.println(" 节点 transType=" + transType + "; outName=" + outName);
            //获取团单信息对象
            Element groupPolicy = root.element("PolicyInfoList").element("PolicyInfo");
            System.out.println(groupPolicy.elementText("PolicyNo"));

            List<Element> elements = root.element("PolicyInfoList").elements("PolicyInfo");
            Element element = elements.get(0);
            System.out.println(element.elementText("PolicyNo"));

        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

}
